package com.googlecode.jmicroerp.wicket.stocuri;


import com.googlecode.jmicroerp.wicket.DefaultViewPage;
import com.googlecode.jmicroerp.wicket.index.IndexPage;
import com.googlecode.jmicroerp.model.Recipient;
import com.googlecode.jmicroerp.service.AprovizionareService;
import com.googlecode.jmicroerp.service.GenericService;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;

@SuppressWarnings("serial")
public class IstoricPage extends DefaultViewPage {
    
    @SpringBean(name = "aprovizionareService")
    private AprovizionareService aprovizionareService;
    @SpringBean(name = "genericService")
    private GenericService<Recipient> recipientService;

    private Date today = new Date(); // data curenta    
    private Date azi = addDays(today, 0); // data azi resetata la miezul noptii
    private Date ieri = addDays(today, -1); // data ieri cu data curenta - o zi

    public static Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        //cal.set(Calendar.AM_PM, Calendar.AM);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    private Locale locale = new Locale("ro");

    private class IstoricView extends ListView<Object[]> {

        public IstoricView(String id, List<Object[]> data) {
            super(id, data);
        }

        @Override
        protected void populateItem(ListItem<Object[]> li) {
            Object[] object = li.getModelObject();
            li.add(new Label("recipient", object[0].toString()));
            li.add(new Label("dataModificarii", DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(object[1])));
            li.add(new Label("utilizator", object[6].toString()));
            li.add(new Label("valoare", object[2].toString()));

        }
    }

    public IstoricPage(Long idRecipient) {
        
        Recipient recipient = recipientService.getById(Recipient.class, idRecipient);
        final Label lbRecipient = new Label("lbRecipient", recipient.getDenumire());
        add(lbRecipient);
        final IstoricView istoricView = new IstoricView("istoricView", aprovizionareService.queryAprovizionare(idRecipient, azi));
        add(istoricView);
      
        add(new Link("inapoi") {

            @Override
            public void onClick() {
                setResponsePage(IndexPage.class);
            }
            
        });
  
    }
}